/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.nct.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.nct.model.entities.Chude;
import org.nct.model.entities.Tacgia;
import org.nct.model.entities.Tintuc;

/**
 *
 * @author haott
 */
public class tinTucProvider implements IModel {

    static final Logger logger = Logger.getLogger(tinTucProvider.class.toString());
    tacGiaProvider _tgProvider;

    @Override
    public List<Object> SelectAll() {
        List<Object> lists;
        try {
            DataProvider.getInstance().Open();
        } catch (SQLException ex) {
            Logger.getLogger(tinTucProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        logger.info("Select all");

        lists = new ArrayList<>();
        String query = "SELECT * FROM TINTUC";

        ResultSet res = DataProvider.getInstance().ExecuteReader(query);
        try {
            while (res.next()) {

                Tintuc _tt = new Tintuc(
                        res.getInt("ID"),
                        res.getBytes("HINHDAIDIEN"),
                        res.getString("TUADE"),
                        res.getString("MOTA"),
                        res.getString("NOIDUNG"),
                        res.getBoolean("TRANGTHAI"),
                        res.getString("NGAYTAO"),
                        res.getString("NGAYCAPNHAT"),
                        res.getBoolean("TRANGCHU"));

                // get Tacgia info
                Tacgia _tg1 = (Tacgia) new tacGiaProvider().SelectOne(res.getInt("ID_TACGIA"));
                _tt.setIdTacgia(_tg1);

                List<Chude> lstChude;
                lstChude = new chuDeProvider().GetListChuDeFromTinTuc(_tt.getId());
                _tt.setChudeList(lstChude);

                lists.add(_tt);
            }
        } catch (SQLException ex) {
            Logger.getLogger(tinTucProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(tinTucProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lists;
    }

    public List<Tintuc> GetListTinTucOnTrangChu() {
        List<Tintuc> lists;
        try {
            DataProvider.getInstance().Open();
        } catch (SQLException ex) {
            Logger.getLogger(tinTucProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        logger.info("Select all");

        lists = new ArrayList<>();
        String query = "SELECT * FROM TINTUC WHERE TRANGCHU=1 AND TRANGTHAI=1";

        ResultSet res = DataProvider.getInstance().ExecuteReader(query);
        try {
            while (res.next()) {
                Tintuc _tt = new Tintuc(
                        res.getInt("ID"),
                        res.getBytes("HINHDAIDIEN"),
                        res.getString("TUADE"),
                        res.getString("MOTA"),
                        res.getString("NOIDUNG"),
                        res.getBoolean("TRANGTHAI"),
                        res.getString("NGAYTAO"),
                        res.getString("NGAYCAPNHAT"),
                        res.getBoolean("TRANGCHU"));

                // get Tacgia info
                Tacgia _tg1 = (Tacgia) new tacGiaProvider().SelectOne(res.getInt("ID_TACGIA"));
                _tt.setIdTacgia(_tg1);

                List<Chude> lstChude;
                lstChude = new chuDeProvider().GetListChuDeFromTinTuc(_tt.getId());
                _tt.setChudeList(lstChude);

                lists.add(_tt);
            }
        } catch (SQLException ex) {
            Logger.getLogger(tinTucProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(tinTucProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lists;
    }

    public List<Tintuc> GetListTinTucFromChuDe(Chude cd) {
        List<Tintuc> lists = null;

        PreparedStatement selectPST = null;

        String query = "SELECT * FROM TINTUC tt "
                + "INNER JOIN CHUDE cd ON tt.ID = cd.ID_TINTUC"
                + "WHERE cd.ID = ?";
        try {
            selectPST = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            selectPST.setInt(1, cd.getId());
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        ResultSet res = DataProvider.getInstance().ExecuteReaderPreparedStatement();
        try {

            Tintuc _tt = new Tintuc(
                    res.getInt("ID"),
                    res.getBytes("HINHDAIDIEN"),
                    res.getString("TUADE"),
                    res.getString("MOTA"),
                    res.getString("NOIDUNG"),
                    res.getBoolean("TRANGTHAI"),
                    res.getString("NGAYTAO"),
                    res.getString("NGAYCAPNHAT"),
                    res.getBoolean("TRANGCHU"));

            // get Tacgia info
            Tacgia _tg1 = (Tacgia) new tacGiaProvider().SelectOne(res.getInt("ID_TACGIA"));
            _tt.setIdTacgia(_tg1);

            _tt.setChudeList((List<Chude>) cd);

            lists.add(_tt);
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            DataProvider.getInstance().Close();

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        return lists;
    }

    public List<Tintuc> GetListTinTucFromTuaDe(String _strTuaDe) {
        List<Tintuc> lists = null;

        PreparedStatement selectPST = null;

        String query = "SELECT * FROM TINTUC WHERE TUADE LIKE CONCAT('%',?,'%')";
        try {
            selectPST = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            selectPST.setString(1, _strTuaDe);
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        ResultSet res = DataProvider.getInstance().ExecuteReaderPreparedStatement();
        try {
            Tintuc _tt = new Tintuc(
                    res.getInt("ID"),
                    res.getBytes("HINHDAIDIEN"),
                    res.getString("TUADE"),
                    res.getString("MOTA"),
                    res.getString("NOIDUNG"),
                    res.getBoolean("TRANGTHAI"),
                    res.getString("NGAYTAO"),
                    res.getString("NGAYCAPNHAT"),
                    res.getBoolean("TRANGCHU"));

            // get Tacgia info
            Tacgia _tg = (Tacgia) new tacGiaProvider().SelectOne(res.getInt("ID_TACGIA"));
            _tt.setIdTacgia(_tg);

            _tt.setChudeList((List<Chude>) new chuDeProvider().GetListChuDeFromTinTuc(_tt.getId()));

            lists.add(_tt);
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            DataProvider.getInstance().Close();

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        return lists;
    }

    public List<Tintuc> GetListTinTucFromTacGia(Tacgia obj) {
        List<Tintuc> lists = null;

        PreparedStatement selectPST = null;

        String query = "SELECT * FROM TINTUC WHERE ID_TACGIA=?";
        try {
            selectPST = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            selectPST.setInt(1, obj.getId());
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        ResultSet res = DataProvider.getInstance().ExecuteReaderPreparedStatement();
        try {
            Tintuc _tt = new Tintuc(
                    res.getInt("ID"),
                    res.getBytes("HINHDAIDIEN"),
                    res.getString("TUADE"),
                    res.getString("MOTA"),
                    res.getString("NOIDUNG"),
                    res.getBoolean("TRANGTHAI"),
                    res.getString("NGAYTAO"),
                    res.getString("NGAYCAPNHAT"),
                    res.getBoolean("TRANGCHU"));

            _tt.setIdTacgia(obj);

            _tt.setChudeList((List<Chude>) new chuDeProvider().GetListChuDeFromTinTuc(_tt.getId()));

            lists.add(_tt);
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            DataProvider.getInstance().Close();

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        return lists;
    }

    @Override
    public Object SelectOne(Object id) {
        Tintuc result = null;

        PreparedStatement selectPST = null;

        String query = "SELECT * FROM TINTUC WHERE ID = ?";
        try {
            selectPST = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            selectPST.setInt(1, Integer.parseInt(id.toString()));
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        ResultSet res = DataProvider.getInstance().ExecuteReaderPreparedStatement();
        try {

            Tintuc _tt = new Tintuc(
                    res.getInt("ID"),
                    res.getBytes("HINHDAIDIEN"),
                    res.getString("TUADE"),
                    res.getString("MOTA"),
                    res.getString("NOIDUNG"),
                    res.getBoolean("TRANGTHAI"),
                    res.getString("NGAYTAO"),
                    res.getString("NGAYCAPNHAT"),
                    res.getBoolean("TRANGCHU"));

            // get Tacgia info
            Tacgia _tg1 = (Tacgia) new tacGiaProvider().SelectOne(res.getInt("ID_TACGIA"));
            _tt.setIdTacgia(_tg1);

            List<Chude> lstChude;
            lstChude = new chuDeProvider().GetListChuDeFromTinTuc(_tt.getId());
            _tt.setChudeList(lstChude);

            result = _tt;
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            DataProvider.getInstance().Close();

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        return result;
    }

    ;
    @Override
    public int Insert(Object obj) {
        int count;

        PreparedStatement statement = null;

        String query = "INSERT TINTUC(ID,HINHDAIDIEN,TUADE,MOTA,NOIDUNG,TRANGTHAI"
                + ",NGAYTAO,NGAYCAPNHAT,TRANGCHU,ID_TACGIA)"
                + " VALUES(?,?,?,?,?,?,?,?,?,?)";

        try {
            statement = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        Tintuc _tt = (Tintuc) obj;

        try {
            statement.setInt(1, _tt.getId());
            statement.setBytes(2, _tt.getHinhdaidien());
            statement.setString(3, _tt.getTuade());
            statement.setString(4, _tt.getMota());
            statement.setString(5, _tt.getNoidung());
            statement.setBoolean(6, _tt.getTrangthai());
            statement.setString(7,_tt.getNgaytao());
            statement.setString(8, _tt.getNgaycapnhat());
            statement.setBoolean(9, _tt.getTrangchu());
            statement.setString(10, _tt.getIdTacgia().getId().toString());

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        count = DataProvider.getInstance().ExecuteNonQueryPreparedStatement();

        // Them vao bang CHU DE 
        chuDeProvider _cdProvider = new chuDeProvider();
        for (Chude _cd : _tt.getChudeList()) {
            List<Tintuc> lstTinTuc = new ArrayList<>();
            lstTinTuc.add((Tintuc) obj);
            _cd.setTintucList(lstTinTuc);
            _cdProvider.Insert(_cd);
        }

        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return count;
    }

    @Override
    public int Update(Object id, Object obj) {
        int count;
        PreparedStatement statement = null;

        String query = "UPDATE TINTUC SET HINHDAIDIEN=?,TUADE=?,MOTA=?,"
                + "NOIDUNG=?,TRANGTHAI=?,NGAYTAO=?,NGAYCAPNHAT=?,TRANGCHU=?,"
                + "ID_TACGIA=? WHERE ID=?";

        try {
            statement = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        Tintuc _tt = (Tintuc) obj;
        try {
            statement.setBytes(1, _tt.getHinhdaidien());
            statement.setString(2, _tt.getTuade());
            statement.setString(3, _tt.getMota());
            statement.setString(4, _tt.getNoidung());
            statement.setBoolean(5, _tt.getTrangthai());
            statement.setString(6, _tt.getNgaytao());
            statement.setString(7, _tt.getNgaycapnhat());
            statement.setBoolean(8, _tt.getTrangchu());
            statement.setInt(9, _tt.getIdTacgia().getId());

            statement.setInt(10, Integer.parseInt(id.toString()));
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        count = DataProvider.getInstance().ExecuteNonQueryPreparedStatement();

        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return count;

    }

    public int UpdateTrangThai(boolean isTrangThai, Tintuc _tt) {
        int count;
        PreparedStatement statement = null;

        String query = "UPDATE TINTUC SET TRANGTHAI=? WHERE ID=?";

        try {
            statement = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            statement.setBoolean(1, isTrangThai);
            statement.setInt(2, _tt.getId());
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        count = DataProvider.getInstance().ExecuteNonQueryPreparedStatement();

        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return count;

    }

    @Override
    public int Delete(Object id) {
        int idTinTuc = Integer.parseInt(id.toString());
        int count;
        PreparedStatement statement = null;

        String query = "DELETE FROM TINTUC WHERE ID=?";

        try {
            statement = DataProvider.getInstance().OpenWithParamater(query);
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            statement.setInt(1, idTinTuc);

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        count = DataProvider.getInstance().ExecuteNonQueryPreparedStatement();
        
        //Xoa tin tuc do trong ban CHU DE 
        new chuDeProvider().DeleteFromTinTuc(idTinTuc);
        
        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return count;
    }

    @Override
    public int increase(Object obj) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int getMaxID() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
